import './styles/index.less';
import {createApp} from 'vue';
import {createPinia} from 'pinia';
import {renderWithQiankun, qiankunWindow, type QiankunProps} from 'vite-plugin-qiankun/dist/helper';
import App from './App.vue';
import router from './router';

function render(props: QiankunProps = {}) {
  const {container} = props;
  const app = createApp(App);
  app.use(createPinia());
  app.use(router);

  app.mount(container ? (container.querySelector('#app') as HTMLElement) : '#app');
}

// 调试：强制打印当前环境
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  console.log('%c 独立渲染', 'color: red; font-size: 20px;');
  render();
}

//  微应用 入口
renderWithQiankun({
  mount(props) {
    console.log('%c qiankun 渲染', 'color: red; font-size: 20px;');
    // console.log('Props', props);
    render(props);

    props.onGlobalStateChange((state, prev) => {
      // state: 变更后的状态; prev 变更前的状态
      console.log('state', state);
      console.log('prev', prev);
    });
  },
  bootstrap() {
    console.log('bootstrap');
  },
  unmount(props) {
    console.log('unmount', props);
  },
  update(props) {
    console.log('update', props);
  },
});
